package com.kr.aia.powerrp.db;

import java.util.ArrayList;
import java.util.List;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

public class CustomerDBUtil {

	// Database fields
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;
	private Context context;
	
	private String[] allColumns = { MySQLiteHelper.COLUMN_CUSTOMER_ID, 
									MySQLiteHelper.COLUMN_CUSTOMER_NAME, 
									MySQLiteHelper.COLUMN_CUSTOMER_RECOMMENDER, 
									MySQLiteHelper.COLUMN_CUSTOMER_RELATION, 
									MySQLiteHelper.COLUMN_CUSTOMER_PHONE_NO
									};

	public CustomerDBUtil(Context context) {
		dbHelper = new MySQLiteHelper(context);
		this.context = context;
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}
	
	
	public long insertCustInfo(CustomerInfoVO custInfo ) {
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COLUMN_CUSTOMER_NAME, custInfo.getCustomerName());
		values.put(MySQLiteHelper.COLUMN_CUSTOMER_RECOMMENDER, custInfo.getRecommender());
		values.put(MySQLiteHelper.COLUMN_CUSTOMER_RELATION, custInfo.getRelation());
		values.put(MySQLiteHelper.COLUMN_CUSTOMER_PHONE_NO, custInfo.getCustomerPhoneNo());
		
		
		long insertId = database.insert(MySQLiteHelper.TABLE_CUSTOMER, null, values);
		
		// To show how to query
		Cursor cursor = database.query(MySQLiteHelper.TABLE_CUSTOMER,
				allColumns, MySQLiteHelper.COLUMN_CUSTOMER_ID + " = " + insertId, null, null, null, null);
		cursor.moveToFirst();
		
		if(insertId > -1){
			Toast.makeText(context, "저장 되었습니다.", Toast.LENGTH_LONG).show();
		}else{
			Toast.makeText(context, "저장 실패!!", Toast.LENGTH_LONG).show();	
		}
		
		return insertId;
		
		/*
		String query =  "INSERT INTO " + MySQLiteHelper.TABLE_COMMENTS + " " +
						" (" + MySQLiteHelper.COLUMN_CUSTOMER_NAME + "," +
						" " + MySQLiteHelper.COLUMN_RECOMMENDER + "," +
						" " + MySQLiteHelper.COLUMN_RELATION + "," +
						" " + MySQLiteHelper.COLUMN_CUSTOMER_PHONE_NO + "" +
						" ) VALUES (" +
						" '" + custInfo.getCustomerName() + "'," +
						" '" + custInfo.getRecommender() + "'," +
						" '" + custInfo.getRelation() + "'," +
						" '" + custInfo.getCustomerPhoneNo() + "'" +
						" )";
		
		Log.e("PRP", query);
		*/
		//database.rawQuery(query, null);
	}
	
	
	public void deleteCustInfo(CustomerInfoVO custInfo) {
		database.delete(MySQLiteHelper.TABLE_CUSTOMER, MySQLiteHelper.COLUMN_CUSTOMER_ID + " = " + custInfo.getSeqno(), null);
	}
	

	public ArrayList<CustomerInfoVO> getAllData() {
		ArrayList<CustomerInfoVO> comments = new ArrayList<CustomerInfoVO>();
		Cursor cursor = database.query(MySQLiteHelper.TABLE_CUSTOMER,
				allColumns, null, null, null, null, MySQLiteHelper.COLUMN_CUSTOMER_NAME + "");
		
		if(cursor.moveToFirst()){    	
	    	do{
	    		CustomerInfoVO custInfo = cursorToComment(cursor);
				comments.add(custInfo);
	    	}while(cursor.moveToNext());
    	}
		// Make sure to close the cursor
		cursor.close();
		return comments;
	}

	private CustomerInfoVO cursorToComment(Cursor cursor) {
		CustomerInfoVO custInfo = new CustomerInfoVO();
		
		custInfo.setSeqno(cursor.getString(0));
		custInfo.setCustomerName(cursor.getString(1));
		custInfo.setRecommender(cursor.getString(2));
		custInfo.setRelation(cursor.getString(3));
		custInfo.setCustomerPhoneNo(cursor.getString(4));
		
		return custInfo;
	}
}